
var QRcodeVal='storage_position,209';
var perm01,perm02,perm03,perm04,perm05;  //权限字段
var openid;

var Strmill=[],Strmill01,role; //当前用户分配的 客户、和客户类型

var code = getQueryString("code");
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


$(function () {
    setTimeout(function() {

        getopenid();

        setgrid();

        $('#dg_file').datagrid({
            data: [],
            striped: true,
            autoSave: false,
            sortable: true,
            fit:true,
            fitColumns:true, //真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动
            rownumbers:true,
            remoteSort: false,
            singleSelect: true,
            showFooter: true,
            idField: 'iid',
            columns:[[
                {field:'iid',title:'主键',width:100,hidden:true},
                {field:'parent_id',title:'主表主键',width:100,hidden:true},
                {field:'filename_original',title:'FileName',width:280,align:'center'},
                {field:'filename_rename',title:'重命名',width:140,align:'center',hidden:true},
                {field:'hash_string',title:'哈希数值',hidden:true},
                {field:'remark',title:'备注',width:140,align:'center',hidden:true},
                {field:'create_time',title:'创建时间',width:160,align:'center',hidden:true},
                {
                    field: 'btn',
                    title: 'Down',
                    width:80,
                    formatter: function(value, row, index) {
                        let result;
                        const lastIndex = row.filename_original.lastIndexOf('.');
                        if (lastIndex !== -1) {
                            result = row.filename_original.substring(lastIndex + 1);
                        }
                        let strfile="https://www.barrel.fit/shunjiebe/drawing/"+row.hash_string +'.'+ result;
                        let link="<a href='"+ strfile +"'" + " target='view window'  download='" +row.filename_original+ "'"+ ">Download</a>";
                        // console.log(link);
                        // return '<button class="btn" onclick="handleClick(' + index + ')">下载</button>';
                        return link;
                    }
                }
            ]]
        });


    },20)
});

function getopenid() {

    $.ajax({
        url: 'https://wxshunjie.barrel.fit/wxbeshunjie/wechat/userMill/' + code,
        type: 'get',
        dataType: 'json',
        // 成功回调
        success: function (cdata) {
            console.log(cdata)
            let data=cdata.data;
            // openid = cdata.data[0].openId;
            // Strmill=cdata.data[0].millSid;
            // role=cdata.data[0].role;

            for (let i = 0; i < data.length; i++) {
                openid = data[0].openId;
                role=data[0].role;
                if (data[i] != null && data[i] !== '') {
                    Strmill.push(data[i].millSid);
                    // if(Strmill){
                    //     Strmill=Strmill+",'"+ data[i].millSid + "'"
                    // }else{
                    //     Strmill="'"+ data[i].millSid + "'";
                    // }
                }
            }

             Strmill01 = Strmill.join('|');

             console.log('openID=' + openid);
             console.log(Strmill01);
            getgriddetail();
            
        },
        //失败回调
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //查看错误信息
            console.log('失败回调');
        }

    })
}


function setgrid() {
    $('#grid_detail').propertygrid({
        data: [],
        nowrap:false,    //自动换行
        showGroup: true,
        scrollbarSize: 1,
        groupField:'headername',
        columns:[[
            {field:'iid',title:'主键',width:100,hidden:true},
            {field:'fieldname',title:'Name',width:100,align:'center'},
            {field:'value',title:'Val',width:150,align:'center'},
            {field:'headername',title:'分组列头',width:120,align:'center',hidden:true},
        ]],
        onDblClickRow: function (Index,row) {//鼠标双击事件
            // console.log('进入双击事件'+row.iid);
        },
        onLoadSuccess:function(data) {
            $('#grid_detail').propertygrid('collapseGroup'); //折叠所有
            // $('#grid_detail').propertygrid('expandGroup',0); //打开第一个
        }
    });
}

function getgriddetail() {
    var td = $("#tdh").val();
    var xh = $("#xh").val(); 
    console.log(td);console.log(xh);
    $.ajax({
        url:' https://wxshunjie.barrel.fit/shunjiebe/dynamicQuery/callProc',
        type: 'post',
        data: JSON.stringify(['usp_ShippingtrackingEnglish',Strmill01,role,td,xh]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            $('#grid_detail').propertygrid('clearSelections');
            $('#grid_detail').propertygrid('loadData', data);

            $('.datagrid-header .datagrid-cell span ').css('font-size','18px');
        },
        complete: function (XMLHttpRequest, textStatus) {
            // console.log("进入到 getData_grid complete");
        }

    })
}

//获取当前行对应的已上传文件名信息
function getdg_flie(vID,billtype) {
    var strSQL="SELECT iid,parent_id,filename_original,filename_rename,hash_string,remark,category,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i')create_time FROM ship_customs_files where parent_id="+ vID;
    $.ajax({
        url: "https://wxshunjie.barrel.fit/shunjiebe/dynamicQuery/querySqlPost",
        // url:' https://www.barrel.fit/shunjiebe/dynamicQuery/querySqlPost',
         type: 'post',
         data: JSON.stringify({sql:strSQL}),
         contentType: "application/json;charset=UTF-8",
       /*
        url:' https://wxshunjie.barrel.fit/shunjiebe/dynamicQuery/callProc',
        type: 'post',
        data: JSON.stringify(['usp_getshipcustomsflie',vID]),
        contentType: "application/json;charset=UTF-8",   
        */     
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            const filteredArray = data.filter(function(obj) {return obj.category === billtype;});
            $('#dg_file').datagrid('clearSelections');
            $('#dg_file').datagrid('loadData', filteredArray);

        },
        complete: function (XMLHttpRequest, textStatus) {

        }

    })
}

function downloadflie(val) {
    var m_ID;
    var currentRow = $("#grid_detail").propertygrid("getSelections");//获得选中行的信息
    // 收货人只能查询运单附件、其他可查看全部
    if (role=='收货人') {
        if (val=='运单') {
            if (currentRow==null || currentRow==undefined || currentRow.length==0) {
                $.messager.alert('tip', 'No data selected!');
                return;
            }else{
                m_ID = currentRow.iid;
                getdg_flie(m_ID,val);
                $('#win_file').window('open')
                $('#grid_detail').datagrid('clearSelections');
            }
        }
    
    }else{
        if (currentRow==null || currentRow==undefined || currentRow.length==0) {
            $.messager.alert('tip', 'No data selected!');
            return;
        }else{
            m_ID = currentRow[0].iid;
            getdg_flie(m_ID,val);
            $('#win_file').window('open')
            $('#grid_detail').datagrid('clearSelections');
        }
    }
   
}